package com.das.mechanic_base.adapter.visit;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.das.mechanic_base.R;
import com.das.mechanic_base.bean.ground.GroundPushBean;
import com.das.mechanic_base.bean.little.LittleAssistantBean;
import com.das.mechanic_base.utils.X3GlideRoundTransform;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class X3LittleAssistantAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context mContext;

    private List<LittleAssistantBean> mList;
    /**
     * 空数据时，显示空布局类型
     */
    private final int EMPTY_VIEW = 1;
    /**
     * 控制空布局的显隐
     */
    private int mEmptyType = 0;

    private final RequestOptions myOptions;
    private int refreshIndex = -1;

    public X3LittleAssistantAdapter(Context mContext) {
        this.mContext = mContext;
        mList = new ArrayList<>();

        mEmptyType = 1;

        myOptions = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                .transform(new X3GlideRoundTransform(mContext, 4));
    }

    /**
     * 下拉刷新
     *
     * @param mList
     */
    public void changeTopData(List<LittleAssistantBean> mList, int refreshIndex) {
        this.mList = mList;
        this.refreshIndex = refreshIndex;
        if (this.mList != null && this.mList.size() > 0) {
            // 显示正常的布局
            mEmptyType = 0;
        } else {
            mEmptyType = 1;
        }

        if (refreshIndex == -1) {
            notifyDataSetChanged();
        } else {
            notifyItemChanged(refreshIndex);
        }
    }

    public void changeData(List<LittleAssistantBean> littleAssistantBean) {
        this.mList = littleAssistantBean;
        if (this.mList != null && this.mList.size() > 0) {
            // 显示正常的布局
            mEmptyType = 0;
        } else {
            mEmptyType = 1;
        }
        notifyDataSetChanged();
    }

    public void changeBottomData(List<LittleAssistantBean> littleAssistantBean) {
        this.mList.addAll(littleAssistantBean);
        if (this.mList != null && this.mList.size() > 0) {
            // 显示正常的布局
            mEmptyType = 0;
        } else {
            mEmptyType = 1;
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == EMPTY_VIEW) {
            return new EmptyHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_empty_post_son_item, parent, false));
        }
        return new AssistantHolder(LayoutInflater.from(mContext).inflate(R.layout.x3_little_assistant_adapter, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {

        if (getItemViewType(position) == 0) {
            AssistantHolder holder1 = (AssistantHolder) holder;
            holder1.content_tv.setText(mList.get(position).getContent());
            holder1.little_title_tv.setText(mList.get(position).getName());
        } else {
            EmptyHolder holder1 = (EmptyHolder) holder;
            holder1.itemView.setOnClickListener(null);
        }
    }

    @Override
    public int getItemCount() {
        return (mList != null && mList.size() > 0) ? mList.size() : mEmptyType;
    }

    IOnClickCustom iOnClickCustom;

    public void setiOnClickCustom(IOnClickCustom iOnClickCustom) {
        this.iOnClickCustom = iOnClickCustom;
    }

    public interface IOnClickCustom {
        // 标记回访
        void iOnClickVisit(GroundPushBean pushBean);
    }

    @Override
    public int getItemViewType(int position) {
        if (mEmptyType == EMPTY_VIEW) {
            //空布局的类型
            return EMPTY_VIEW;
        }
        return super.getItemViewType(position);
    }

    class AssistantHolder extends RecyclerView.ViewHolder {
        private TextView content_tv;
        private TextView little_title_tv;

        public AssistantHolder(@NonNull View itemView) {
            super(itemView);
            content_tv = itemView.findViewById(R.id.content_tv);
            little_title_tv = itemView.findViewById(R.id.little_title_tv);
        }
    }

    class EmptyHolder extends RecyclerView.ViewHolder {
        TextView tv_empty;

        public EmptyHolder(@NonNull View itemView) {
            super(itemView);
            tv_empty = itemView.findViewById(R.id.tv_empty);
        }
    }
}
